package ch.zbw.swt.sysventory.network;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.EnumVariant;
import com.jacob.com.Variant;

public class ServerIpRead {
	private String ipAddress, subnetMask;

	public ServerIpRead() {

		this.getIpCIDR();
	}

	public void getIpCIDR() {

		String host = "localhost";
		String connectStr = String.format("winmgmts:\\\\%s\\root\\CIMV2", host);
		String query = "SELECT * FROM Win32_NetworkAdapterConfiguration";

		ActiveXComponent axWMI = new ActiveXComponent(connectStr);

		Variant vCollection = axWMI.invoke("ExecQuery", new Variant(query));

		// Das Resultat ist eine Datensammlung
		EnumVariant enumVariant = new EnumVariant(vCollection.toDispatch());
		Dispatch item = null;
		while (enumVariant.hasMoreElements()) {
			item = enumVariant.nextElement().toDispatch();

			if (!Dispatch.call(item, "IPSubnet").toString().equals("null")
					&& !Dispatch.call(item, "IPAddress").equals("null")) {

				subnetMask = Dispatch.call(item, "IPSubnet").toString()
						.substring(1);
				ipAddress = Dispatch.call(item, "IPAddress").toString()
						.substring(1);

				subnetMask = subnetMask.substring(0, subnetMask.indexOf(" "));
				ipAddress = ipAddress.substring(0, ipAddress.indexOf(" "));

			}
		}

	}

	public String getSubnetMask() {
		return subnetMask;
	}

	public void setSubnetMask(String subnetMask) {
		this.subnetMask = subnetMask;
	}

	public String getIpAddress() {
		return ipAddress;
	}

	public void setIpAddress(String ipAddress) {
		this.ipAddress = ipAddress;
	}
}
